//
//  ForgetPwdViewController.m
//  EasyGo
//
//  Created by newhuidong on 16/1/19.
//  Copyright © 2016年 HuiDong. All rights reserved.
//

#import "ForgetPwdViewController.h"
#import "UIButton+CountDown.h"
#import "AFNetworking.h"
#import "URL.h"
#import "AppDelegate.h"
@interface ForgetPwdViewController (){
    
    
    
    UIView * bgView;
    
    UILabel * phoneLabel;
    UILabel * codeLabel;
    UILabel * newPswLabel;
    
    UIImageView * phoneImageView;
    UIImageView * codeImageVIew;
    UIImageView * newPswImageView;
    
    
    UITextField * codeTF;
    UITextField * newpswTF;
    
    
}
@property (nonatomic,strong)UIButton * codeButton;
@property(nonatomic,strong)UITextField * phoneTF;;
@end

@implementation ForgetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"忘记密码";
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:0/255.0f green:128/255.0f blue:255/255.0f alpha:1];
    
    self.navigationController.navigationBar.tintColor=[UIColor whiteColor];
    
    [[UIBarButtonItem appearance]setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
    self.view.backgroundColor=[UIColor groupTableViewBackgroundColor];
    [self loadUI];
    [self sendCode];
}
-(void)viewWillAppear:(BOOL)animated{
    
    
    self.navigationController.navigationBar.translucent=NO;
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init]  forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    
    
    
}

-(void)loadUI{
    
    CGRect frame=[UIScreen mainScreen].bounds;
    
    bgView=[[UIView alloc]initWithFrame:CGRectMake(0, 84, frame.size.width, 150  )];
    bgView.backgroundColor=[UIColor whiteColor];
    bgView.layer.borderColor=[UIColor groupTableViewBackgroundColor].CGColor;
    bgView.layer.borderWidth=1;
    
    UIView * line1=[[UIView alloc]initWithFrame:CGRectMake(0, 50, frame.size.width, 1)];
    line1.backgroundColor=[UIColor groupTableViewBackgroundColor];
    
    UIView * line2=[[UIView alloc]initWithFrame:CGRectMake(0, 100, frame.size.width, 1)];
    line2.backgroundColor=[UIColor groupTableViewBackgroundColor];
    
    phoneLabel=[[UILabel alloc]initWithFrame:CGRectMake(40, 10, 40, 30)];
    phoneLabel.text=@"+86";
    phoneLabel.textColor=[UIColor grayColor];

    
    codeLabel=[[UILabel alloc]initWithFrame:CGRectMake(40, 60, 60, 30)];
    codeLabel.text=@"验证码";
    codeLabel.textColor=[UIColor grayColor];
    
    
    newPswLabel=[[UILabel alloc]initWithFrame:CGRectMake(40, 110, 60, 30)];
    newPswLabel.text=@"新密码";
    newPswLabel.textColor=[UIColor grayColor];
    
    _phoneTF=[[UITextField alloc]initWithFrame:CGRectMake(100, 10, frame.size.width-80-80, 30)];
    _phoneTF.placeholder=@"请输入手机号";
    _phoneTF.font=[UIFont systemFontOfSize:15];
    _phoneTF.keyboardType=UIKeyboardTypeNumberPad;
    
    codeTF=[[UITextField alloc]initWithFrame:CGRectMake(100, 60, frame.size.width-80, 30)];
    codeTF.placeholder=@"请输入验证码";
    codeTF.keyboardType=UIKeyboardTypeNumberPad;
    codeTF.clearButtonMode=UITextFieldViewModeAlways;
    codeTF.font=[UIFont systemFontOfSize:15];
    
    
    newpswTF=[[UITextField alloc]initWithFrame:CGRectMake(100, 110, frame.size.width-80, 30)];
    newpswTF.placeholder=@"设置密码,6-20位数字字母组合";
    newpswTF.font=[UIFont systemFontOfSize:15];
    
    
    
    
    
    [bgView addSubview:_phoneTF];
    [bgView addSubview:codeTF];
    [bgView addSubview:newpswTF];
    
    [bgView addSubview:line1];
    [bgView addSubview:line2];
    [bgView addSubview:phoneLabel];

    [bgView addSubview:codeLabel];
    [bgView addSubview:newPswLabel];
    
    UIButton * submitButton =[UIButton buttonWithType:UIButtonTypeCustom];
    submitButton.frame=CGRectMake(20, frame.size.width, frame.size.width-40, 40);
    [submitButton setBackgroundColor:[UIColor colorWithRed:0/255.0f green:128/255.0f blue:255/255.0f alpha:1]];
    [submitButton setTitle:@"提交" forState:0];
    [submitButton setTitleColor:[UIColor whiteColor] forState:0];
    [submitButton addTarget:self action:@selector(submitButton) forControlEvents:UIControlEventTouchUpInside];
    submitButton.layer.cornerRadius=4;
    
    [self.view addSubview:submitButton];
    [self.view addSubview:bgView];
    
    
    
}
-(void)sendCode{
    
    
    CGRect frame=[UIScreen mainScreen].bounds;
    
    UIButton * button =[UIButton buttonWithType:UIButtonTypeCustom];
    
    
    //button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(frame.size.width-80, 0, 80,50 );
    [button setTitle:@"获取验证码" forState:0];
    button.titleLabel.font=[UIFont systemFontOfSize:13];
    [button setTitleColor:[UIColor blackColor] forState:0];
    button.layer.borderColor=[UIColor groupTableViewBackgroundColor].CGColor;
    button.layer.borderWidth=1;
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:button];

    
    
}
//获取验证码
-(void)buttonAction:(UIButton *)button{
    
    
    NSLog(@"倒计时");
    [button startWithTime:10 title:@"重新获取" countDownTitle:@"s" mainColor:[UIColor whiteColor] countColor:[UIColor whiteColor]];
    
    [self sendMessage];
    
    
}
-(void)sendMessage{
    
    AFHTTPSessionManager * manager=[AFHTTPSessionManager manager];
    
    manager.responseSerializer=[AFJSONResponseSerializer serializer];
    
    NSMutableDictionary * dic=[[NSMutableDictionary alloc]init];
    
    AppDelegate* tempDelgate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    dic[@"token"]=tempDelgate.token;
    
    dic[@"phone"]=_phoneTF.text;
    
    NSString * urlString=[NSString stringWithFormat:@"%@%@",url1,@"ForgetSMS"];
    
    [manager POST:urlString parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary * dic=(NSDictionary *)responseObject;
        
        NSLog(@"忘记密码:%@",[dic description]);
        
       // NSString * string=dic[@"Message"];
        
        //NSString * string1=[string  stringByAddingPercentEncodingWithAllowedCharacters:string];
        
      //  NSLog(@"汉字:%@",string1);
        
    
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"Error:%@",error);
    }];
    
    
    
    
    
    
    
    
    
    
    
    
    
}
-(void)timerFire:(NSTimer*)timer{
    
    self.codeButton.titleLabel.text=@"10";
    
    

}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    
    [self.view endEditing:YES];
}
-(void)submitButton{
    
    
    NSLog(@"提交");
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}
@end
